<div class="filter">
    <div class="f3 item" style="width: 120px;">
        <select data-am-selected>
        </select>
    </div>
    <div class="f1 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="起始日期" id="start"
            data-am-datepicker="{theme:'success'}" readonly />
    </div>
    <div class="f2 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="结束日期" id="end"
            data-am-datepicker="{theme:'success'}" readonly />
    </div>
    <div class="button item">
        <button type="button" class="am-btn am-btn-primary btn-query">查看</button>
    </div>
</div>

<div style="line-height: 36px;" class="red">
    【操作提示】未列出的彩种，表示当期没有任何有效投注。
</div>
<table class="am-table am-table-bordered am-table-striped" id="result-table">
    <thead>
        <tr>
            <th>彩种</th>
            <th>下注总额</th>
            <th>中奖总额</th>
            <th>输赢总额</th>
            <th>返水总额</th>
            <th>注单数</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<script>
    $(".f1 input").datepicker({ format: 'yyyy-mm-dd' });
    $(".f2 input").datepicker({ format: 'yyyy-mm-dd' });
    $(".f1 input").datepicker("setValue", new Date().Format("yyyy-MM-dd"));
    $(".f2 input").datepicker("setValue", new Date().Format("yyyy-MM-dd"));

    var arr = get_game_list();
    $(".f3 select").append('<option value="-1">所有彩种</option>');
    for (var i in arr) {
        var line = '<option value="__v__">__name__</option>';
        line = line.replace(/__v__/, arr[i]);
        line = line.replace(/__name__/, get_game_display_name(arr[i]));
        $(".f3 select").append(line);
    }
    Util.restorePageFilter();

    function queryWinLoseDetail() {
        var dateS = $(".f1 input").val(), dateE = $(".f2 input").val();
        var type = $(".f3 select").val();
        if (type == '-1') type = undefined;

        var delta = new Date(dateE).getTime() - new Date(dateS).getTime();
        if (delta > 31 * 24 * 3600 * 1000) {
            alert2("查询的时间跨度，不能超过1个月！", true);
            return;
        }

        Util.savePageFilter([".f1 input", ".f2 input", ".f3 select"]);
        startLoading();
        CMD.bet_sum_query(type, dateS, dateE, "#SUM#", function(r) {
            stopLoading();
            fillResults(r);
        });
    }

    function fillResults(list) {
        // 将结果转换下
        var result = {};
        for (var i in list) {
            var d = list[i];
            if (!result[d.type]) result[d.type] = d;
            else {
                // 合并下
                for (var k in d) {
                    if (k == 'date' || k == 'id' || k == 'owner' || k == 'type') continue;
                    result[d.type][k] += d[k];
                }
            }
        }

        var table = $(".am-table tbody");
        table.empty();
        var sum;
        for (var i in result) {
            var d = result[i];
            if (d.type == '#SUM#') {
                sum = d;
                continue;
            }

            var tr = $("<tr></tr>");
            tr.append("<td>" + get_game_display_name(d.type) + "</td>");
            tr.append("<td>" + (d.amount/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.win/100).toFixed(2) + "</td>");
            if (d.profit >= 0)
                tr.append("<td class='red'>" + (d.profit/100).toFixed(2) + "</td>");
            else
                tr.append("<td class='green'>" + (d.profit/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.back/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.count || 0) + "</td>");
            table.append(tr);
        }

        var d = sum;
        if (!d || $(".f3 select").val() != -1) return;
        tr = $("<tr></tr>");
        tr.append("<td>合计</td>");
        tr.append("<td>" + (d.amount/100).toFixed(2) + "</td>");
        tr.append("<td>" + (d.win/100).toFixed(2) + "</td>");
        if (d.profit >= 0)
            tr.append("<td class='red'>" + (d.profit/100).toFixed(2) + "</td>");
        else
            tr.append("<td class='green'>" + (d.profit/100).toFixed(2) + "</td>");
        tr.append("<td>" + (d.back/100).toFixed(2) + "</td>");
        tr.append("<td>" + (d.count || 0) + "</td>");
        table.append(tr);
    }

    $(".btn-query").click(function() {
        queryWinLoseDetail();
    });
    queryWinLoseDetail();
</script>